package yw.nmh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import yw.nmh.dicdata.domain.DicDataInfo;
import yw.nmh.exception.BusinessException;
import yw.nmh.mapper.DicDataInfoMapper;
import yw.nmh.mapper.RanchInfoMapper;
import yw.nmh.ranchInfo.domain.Ranch;
import yw.nmh.ranchInfo.dto.RanchAddDTO;
import yw.nmh.ranchInfo.dto.RanchBaseDTO;
import yw.nmh.ranchInfo.dto.RanchUpdateDTO;
import yw.nmh.ranchInfo.vo.RanchBaseVO;
import yw.nmh.ranchInfo.vo.RanchVO;
import yw.nmh.result.PageDTO;
import yw.nmh.service.DicDataInfoService;
import yw.nmh.service.RanchInfoService;
import yw.nmh.utils.BeanConverter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class RanchInfoServiceImpl extends ServiceImpl<RanchInfoMapper, Ranch> implements RanchInfoService {

    @Resource
    private RanchInfoMapper ranchInfoMapper;

    //插入字典的service
    @Resource
    private DicDataInfoService dicDataInfoService;

    @Resource
    private DicDataInfoMapper dicDataInfoMapper;
    //按照需求查询牧场
    @Override
    public Page<RanchBaseVO> listRanch(RanchBaseDTO ranchBaseDTO) {
        LambdaQueryWrapper<Ranch> ranchLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ranchLambdaQueryWrapper.like(Ranch::getName,ranchBaseDTO.getName())
                .eq(Ranch::getStatus,ranchBaseDTO.getStatus());
        PageDTO pageDTO = new PageDTO();
        BeanUtils.copyProperties(ranchBaseDTO.getPageDTO(),pageDTO);
        Page<Ranch> ranchPage = new Page<>(pageDTO.getPageNumber(), pageDTO.getPageSize());
        Page<Ranch> page = this.page(ranchPage, ranchLambdaQueryWrapper);

        Page<RanchBaseVO> ranchBaseVOPage = new Page<RanchBaseVO>();
        ranchBaseVOPage.setRecords(BeanConverter.convertList(page.getRecords(),RanchBaseVO.class));
        ranchBaseVOPage.setTotal(page.getTotal());
        ranchBaseVOPage.setCurrent(page.getCurrent());
        ranchBaseVOPage.setSize(page.getSize());

        return ranchBaseVOPage;

    }
//查询单个牧场详情
    @Override
    public RanchVO ranch(Integer id) {
        Ranch ranch = ranchInfoMapper.selectById(id);
        RanchVO ranchVO = new RanchVO();
        ranchVO.setName(ranch.getName());
        ranchVO.setArea(ranch.getArea());
        ranchVO.setBreedingScale(ranch.getBreedingScale());
        ranchVO.setAddress(ranch.getProvince() + ranch.getDetailedAddress());
        String valValue = dicDataInfoService.One(ranch.getValId()).getValValue();
        ranchVO.setType(valValue);
        return ranchVO;
    }

    @Override
    public Boolean addRanch(RanchAddDTO ranchAddDTO) {
        DicDataInfo dicDataInfo = dicDataInfoMapper.selectById(ranchAddDTO.getValId());
        if (dicDataInfo.getIsEnable() == 0){
            return false;
        }
        checkNameExists(ranchAddDTO.getName(),null);
        Ranch ranch = new Ranch();
        ranch.setName(ranchAddDTO.getName());
        ranch.setValId(ranchAddDTO.getValId());
        ranch.setArea(ranchAddDTO.getArea());
        ranch.setCity(ranchAddDTO.getCity());
        ranch.setBreedingScale(ranchAddDTO.getBreedingScale());
        ranch.setProvince(ranchAddDTO.getProvince());
        ranch.setLatitude(ranchAddDTO.getLatitude());
        ranch.setLongitude(ranchAddDTO.getLongitude());
        ranch.setDistrict(ranchAddDTO.getDistrict());
        ranch.setDetailedAddress(ranchAddDTO.getDetailedAddress());
        ranch.setCreatedTime(new Date());
        ranch.setUpdatedTime(new Date());
        ranchInfoMapper.insert(ranch);
        return true;
    }

    @Override
    public Boolean updateRanch(RanchUpdateDTO ranchUpdateDTO) {
        DicDataInfo dicDataInfo = dicDataInfoMapper.selectById(ranchUpdateDTO.getValId());
        if (dicDataInfo.getIsEnable() == 0){
            return false;
        }
        checkNameExists(ranchUpdateDTO.getName(),null);
        Ranch ranch = ranchInfoMapper.selectById(ranchUpdateDTO.getId());
        ranch.setName(ranchUpdateDTO.getName());
        ranch.setValId(ranchUpdateDTO.getValId());
        ranch.setArea(ranchUpdateDTO.getArea());
        ranch.setCity(ranchUpdateDTO.getCity());
        ranch.setBreedingScale(ranchUpdateDTO.getBreedingScale());
        ranch.setProvince(ranchUpdateDTO.getProvince());
        ranch.setLatitude(ranchUpdateDTO.getLatitude());
        ranch.setLongitude(ranchUpdateDTO.getLongitude());
        ranch.setDistrict(ranchUpdateDTO.getDistrict());
        ranch.setDetailedAddress(ranchUpdateDTO.getDetailedAddress());
        ranch.setUpdatedTime(new Date());
        ranch.setCreatedTime(ranch.getCreatedTime());
        ranchInfoMapper.updateById(ranch);
        return true;
    }


    private void checkNameExists(String name, Integer excludeId) {
        LambdaQueryWrapper<Ranch> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Ranch::getName, name);

        if (excludeId != null) {
            wrapper.ne(Ranch::getId, excludeId);
        }

        if (count(wrapper) > 0) {
            throw new BusinessException("牧场名称已存在");
        }
    }

}
